package com.ler.yrmall.product.rpc;

import cn.hutool.core.bean.BeanUtil;
import com.ler.yrmall.product.api.ProductApi;
import com.ler.yrmall.product.dto.SkuDTO;
import com.ler.yrmall.product.dto.SpuDTO;
import com.ler.yrmall.product.entity.SkuEntity;
import com.ler.yrmall.product.entity.SpuEntity;
import com.ler.yrmall.product.service.SkuAttrValueService;
import com.ler.yrmall.product.service.SkuService;
import com.ler.yrmall.product.service.SpuService;
import com.ler.yrmall.basic.Result;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Leron
 * @version 1.0
 */
@DubboService
@Service
public class ProductApiServiceImpl implements ProductApi {

    @Resource
    private SpuService spuService;

    @Resource
    private SkuService skuService;

    @Resource
    private SkuAttrValueService skuSaleAttrValueService;

    @Override
    public Result<SpuDTO> getSpuById(long spuId) {
        SpuEntity spu = spuService.getById(spuId);
        SpuDTO target = new SpuDTO();
        BeanUtils.copyProperties(spu,target);
        return Result.ok(target);

    }

    @Override
    public Result<SkuDTO> getSkuById(Long skuId) {
        SkuEntity entity = skuService.getById(skuId);
        SkuDTO target = new SkuDTO();
        BeanUtils.copyProperties(entity,target);
        return Result.ok(target);
    }

    @Override
    public Result<List<String>> getAttrsAsString(Long skuId) {
        return Result.ok(skuSaleAttrValueService.getAttrsAsString(skuId));
    }

    @Override
    public Result<List<SkuDTO>> listSkuByIds(List<Long> skuIds) {
        List<SkuEntity> skuList = skuService.listByIds(skuIds);
        List<SkuDTO> skuDtoList = BeanUtil.copyToList(skuList, SkuDTO.class);
        return Result.ok(skuDtoList);
    }

    @Override
    public Result<List<SpuDTO>> listSpuByIds(List<Long> spuIds) {
        List<SpuEntity> list = spuService.listByIds(spuIds);
        List<SpuDTO> dtoList = BeanUtil.copyToList(list, SpuDTO.class);
        return Result.ok(dtoList);
    }
}
